<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Vue,Vue-cli,Vuex," />





  <link rel="alternate" href="/atom.xml" title="OBKoro1's Blog" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="写在前面：这篇文章是在vue-cli里面使用vuex的一个极简demo，附带一些vuex的简单介绍。有需要的朋友可以做一下参考，喜欢的可以点波赞，或者关注一下，希望可以帮到大家。  本文首发于我的个人blog：obkoro1.com  引入步骤我创建了一个新的vue-cli里面什么东西都没有，只引用了vuex，这里是码云地址,可以下载下来，然后npm install、npm run dev试试看，">
<meta name="keywords" content="Vue,Vue-cli,Vuex">
<meta property="og:type" content="article">
<meta property="og:title" content="手摸手教你在vue-cli里面使用vuex，以及vuex简介">
<meta property="og:url" content="http://yoursite.com/2017/12/09/手摸手教你在vue-cli里面使用vuex，以及vuex简介/index.html">
<meta property="og:site_name" content="OBKoro1&#39;s Blog">
<meta property="og:description" content="写在前面：这篇文章是在vue-cli里面使用vuex的一个极简demo，附带一些vuex的简单介绍。有需要的朋友可以做一下参考，喜欢的可以点波赞，或者关注一下，希望可以帮到大家。  本文首发于我的个人blog：obkoro1.com  引入步骤我创建了一个新的vue-cli里面什么东西都没有，只引用了vuex，这里是码云地址,可以下载下来，然后npm install、npm run dev试试看，">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1603a90a59fc5676?w=363&h=335&f=png&s=16366?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1603ac7ca073cb0e?w=765&h=427&f=png&s=49028?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1603acd24b3c1906?w=773&h=875&f=png&s=97062?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1603ae6d33eae6db?w=554&h=570&f=png&s=153482?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1603ae858f7da6cd?w=789&h=530&f=png&s=148466?raw=true">
<meta property="og:updated_time" content="2019-06-15T06:19:47.658Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="手摸手教你在vue-cli里面使用vuex，以及vuex简介">
<meta name="twitter:description" content="写在前面：这篇文章是在vue-cli里面使用vuex的一个极简demo，附带一些vuex的简单介绍。有需要的朋友可以做一下参考，喜欢的可以点波赞，或者关注一下，希望可以帮到大家。  本文首发于我的个人blog：obkoro1.com  引入步骤我创建了一个新的vue-cli里面什么东西都没有，只引用了vuex，这里是码云地址,可以下载下来，然后npm install、npm run dev试试看，">
<meta name="twitter:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1603a90a59fc5676?w=363&h=335&f=png&s=16366?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/12/09/手摸手教你在vue-cli里面使用vuex，以及vuex简介/"/>





  <title>手摸手教你在vue-cli里面使用vuex，以及vuex简介 | OBKoro1's Blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?64d57d0b926bbd27431daa89d2e0a76b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">OBKoro1's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-前端算法">
          <a href="/arithmetic/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br />
            
            前端算法
          </a>
        </li>
      
        
        <li class="menu-item menu-item-博客最新更新">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            博客最新更新
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/12/09/手摸手教你在vue-cli里面使用vuex，以及vuex简介/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="OBKoro1">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/hand1.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="OBKoro1's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">手摸手教你在vue-cli里面使用vuex，以及vuex简介</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-09T19:15:18+08:00">
                2017-12-09
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Vue/" itemprop="url" rel="index">
                    <span itemprop="name">Vue</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/12/09/手摸手教你在vue-cli里面使用vuex，以及vuex简介/" class="leancloud_visitors" data-flag-title="手摸手教你在vue-cli里面使用vuex，以及vuex简介">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,371 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  5分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="写在前面："><a href="#写在前面：" class="headerlink" title="写在前面："></a>写在前面：</h2><p>这篇文章是在vue-cli里面使用vuex的一个极简demo，附带一些vuex的简单介绍。有需要的朋友可以做一下参考，喜欢的可以点波赞，或者关注一下，希望可以帮到大家。</p>
<blockquote>
<p>本文首发于我的个人blog：<a href="http://obkoro1.com/" target="_blank" rel="noopener">obkoro1.com</a></p>
</blockquote>
<h2 id="引入步骤"><a href="#引入步骤" class="headerlink" title="引入步骤"></a>引入步骤</h2><p>我创建了一个新的vue-cli里面什么东西都没有，只引用了vuex，这里是<a href="https://gitee.com/OBKoro1/blog_vuex" target="_blank" rel="noopener">码云地址</a>,可以下载下来，然后<code>npm install</code>、<code>npm run dev</code>试试看，里面vuex的使用地方也全都注释了一遍。</p>
<h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><pre><code>npm install vuex --save
</code></pre><h3 id="在src目录下创建文件夹vuex"><a href="#在src目录下创建文件夹vuex" class="headerlink" title="在src目录下创建文件夹vuex"></a>在src目录下创建文件夹vuex</h3><p><img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1603a90a59fc5676?w=363&amp;h=335&amp;f=png&amp;s=16366?raw=true" alt="文件夹包含文件"></p>
<p>该文件夹包含以上文件，创建好了之后，我们一个一个文件来说里面都有什么东西。</p>
<h4 id="vuex-index-js"><a href="#vuex-index-js" class="headerlink" title="vuex/index.js"></a>vuex/index.js</h4><pre><code>import Vue from &apos;vue&apos;;
import Vuex from &apos;vuex&apos;;
import status from &apos;./modules/status/index&apos;;//引进模块
Vue.use(Vuex);

export default new Vuex.Store({
  modules: {  
    //Vuex 允许我们将 store 分割成模块（module）。每个模块拥有自己的 state、mutation、action
    dataStatus:status,//访问这里面数据的时候要使用&apos;dataStatus&apos;
  },
});
</code></pre><p>这是vuex的主文件store，这个例子是把state、mutation、action分割成模块，然后再将每个模块引用进这个index.js文件里面，等我们整个文件夹的文件都搭好之后，还要把文件引入到main.js里面、</p>
<h3 id="vuex-modules-status-index-js"><a href="#vuex-modules-status-index-js" class="headerlink" title="vuex/modules/status/index.js"></a>vuex/modules/status/index.js</h3><pre><code>import actions from &apos;./actions&apos;;
import mutations from &apos;./mutations&apos;;
//引入action和mutations 

export default {  
  state:{ //这里面是要读取或者写入数据的地方
      msg:&apos;默认状态&apos;,
  },
  //state actions mutations顺序不能乱
  actions: actions,
  mutations: mutations,
}
</code></pre><p>这是一个模块里面的主文件，模块内部拥有自己的state、actions、mutions，是从上到下进行分割的。</p>
<h3 id="vuex-modules-status-mutation-type-js"><a href="#vuex-modules-status-mutation-type-js" class="headerlink" title="vuex/modules/status/mutation_type.js"></a>vuex/modules/status/mutation_type.js</h3><pre><code>//这个js文件里面只是一些变量，把action和mutation文件里面相同变量名的链接起来
export const VUEX_TEST = &apos;VUEX_TEST&apos;;
// 一般使用的是大写来命名变量，因为尤大也是这么做 2333
</code></pre><h3 id="vuex-modules-status-actions-js"><a href="#vuex-modules-status-actions-js" class="headerlink" title="vuex/modules/status/actions.js"></a>vuex/modules/status/actions.js</h3><pre><code>import * as types from &apos;./mutation_type&apos;; //引入变量
export default {
  actionFn({commit},data){ //actionFn是在组件通过dispatch触发的函数名 可以理解成组件和actions的连接
    commit(types.VUEX_TEST,data);
    //types.VUEX_TEST 是要commit到mutation的哪个位置 变量的作用
    //data 是传过来的参数
  }
};
</code></pre><h3 id="vuex-modules-status-mutations-js"><a href="#vuex-modules-status-mutations-js" class="headerlink" title="vuex/modules/status/mutations.js"></a>vuex/modules/status/mutations.js</h3><pre><code>import * as types from &apos;./mutation_type&apos;;//引入变量

export default {
  //types.VUEX_TEST 代表接受哪个actions的commit 也就是上面引入变量的作用
  [types.VUEX_TEST](state,data){ 
    //第一个参数state是这个模块的state 第二个参数是传进来的数据 
    if(data.status==1){ //根据传进来的参数做各种操作
      //这里就是操作state了，赋值之后，各个组件上面引用该数据的地方会自动更新      
      state.msg=data.text;
    }else if(data.status==2){
        state.msg=&apos;奕迅&apos;;
    }
    //这里是随便写的一个栗子
    console.log(state.mg,data,&apos;mutation&apos;); 
  }
};
</code></pre><p>这两个文件里面有注释，解释的蛮清楚的。当把所有文件创建好了之后，再回过头来看看每个文件之间都有联系,多想想或许就懂点什么东西了 emmm</p>
<h4 id="vuex文件夹，文件目录以及下载"><a href="#vuex文件夹，文件目录以及下载" class="headerlink" title="vuex文件夹，文件目录以及下载"></a><a href="https://gitee.com/OBKoro1/blog_vuex/tree/master/src/vuex" target="_blank" rel="noopener">vuex文件夹</a>，文件目录以及下载</h4><h3 id="引入main-js"><a href="#引入main-js" class="headerlink" title="引入main.js"></a>引入main.js</h3><p><img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1603ac7ca073cb0e?w=765&amp;h=427&amp;f=png&amp;s=49028?raw=true" alt=""></p>
<p>上面的vuex文件夹只是搭建一个store，但是这样还不够，我们还要在项目里面使用这个。引用到main.js，如图所示。</p>
<h3 id="组件中如何使用。"><a href="#组件中如何使用。" class="headerlink" title="组件中如何使用。"></a>组件中如何使用。</h3><p><img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1603acd24b3c1906?w=773&amp;h=875&amp;f=png&amp;s=97062?raw=true" alt=""></p>
<p>使用的话就是像上面那么使用，本文只是一个简单的示例，还有更多骚操作，等大家上手之后再慢慢摸索咯。</p>
<h2 id="git地址"><a href="#git地址" class="headerlink" title="git地址"></a>git地址</h2><p>感觉写的蛮乱的，大家如果看不太懂的话，可以到<a href="https://gitee.com/OBKoro1/blog_vuex" target="_blank" rel="noopener">码云</a>去下载文件，然后自己跑一跑，多看看，试一试就应该没问题了。</p>
<h2 id="vuex简介"><a href="#vuex简介" class="headerlink" title="vuex简介"></a>vuex简介</h2><p>通常我是希望大家先学会用，然后再了解后面的机制，每次我看博文的时候，一大段原理贴上来，都给我弄懵逼了。。</p>
<p>vuex主要是用来<strong>复杂项目之间的组件通信功能</strong>，简单的项目不要用这套复杂的事件以及状态管理机制。项目如果不够复杂的话推荐我之前写的：<a href="https://juejin.im/post/59eddbfe6fb9a0450908abb4" target="_blank" rel="noopener">在vue项目中 如何定义全局变量 全局函数</a>，使用全局变量的形式的也可以实现需求。</p>
<h3 id="vuex实现的作用：数据共享机制"><a href="#vuex实现的作用：数据共享机制" class="headerlink" title="vuex实现的作用：数据共享机制"></a>vuex实现的作用：数据共享机制</h3><p>通过统一的数据中心store维护状态数据，每个组件进行更新的时候，通知数据中心store。再由stroe将共享的状态，触发每一个调用它的组件的更新。</p>
<p><img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1603ae6d33eae6db?w=554&amp;h=570&amp;f=png&amp;s=153482?raw=true" alt="store触发组件更新"></p>
<h3 id="vuex的工作流程"><a href="#vuex的工作流程" class="headerlink" title="vuex的工作流程"></a>vuex的工作流程</h3><p>大家先仔细看看下面这张图，理解他的工作机制。</p>
<p><img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1603ae858f7da6cd?w=789&amp;h=530&amp;f=png&amp;s=148466?raw=true" alt=""></p>
<ol>
<li>在vue组件里面，通过dispatch来触发actions提交修改数据的操作。</li>
<li>然后再通过actions的commit来触发mutations来修改数据。</li>
<li>mutations接收到commit的请求，就会自动通过Mutate来修改state（数据中心里面的数据状态）里面的数据。</li>
<li>最后由store触发每一个调用它的组件的更新</li>
</ol>
<h3 id="注意：这套模型是单向流动的"><a href="#注意：这套模型是单向流动的" class="headerlink" title="注意：这套模型是单向流动的"></a>注意：这套模型是单向流动的</h3><h2 id="后话"><a href="#后话" class="headerlink" title="后话"></a>后话</h2><p>以上就是本文的所有内容了，希望可以帮到大家。</p>
<p><strong>最后</strong>：如需转载，请放上原文链接并署名。码字不易，<strong>感谢</strong>支持！本人写文章本着交流记录的心态，写的不好之处，不撕逼，但是欢迎指点。然后就是希望看完的朋友点个<strong>喜欢</strong>，也可以<strong>关注</strong>一下我。<br><strong><a href="http://obkoro1.com/" target="_blank" rel="noopener">个人blog</a></strong> and <strong><a href="https://juejin.im/user/58714f0eb123db4a2eb95372" target="_blank" rel="noopener">掘金个人主页</a></strong>  </p>
<p>以上2017.12.9</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>听说，打赏我的人最后都找到了真爱。</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/pay/weixin.jpg" alt="OBKoro1 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/pay/ali.jpg" alt="OBKoro1 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Vue/" rel="tag"><i class="fa fa-tag"></i> Vue</a>
          
            <a href="/tags/Vue-cli/" rel="tag"><i class="fa fa-tag"></i> Vue-cli</a>
          
            <a href="/tags/Vuex/" rel="tag"><i class="fa fa-tag"></i> Vuex</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/11/25/cookie、localStorage和sessionStorage 三者之间的区别以及存储、获取、删除等使用方式/" rel="next" title="cookie、localStorage和sessionStorage 三者之间的区别以及存储、获取、删除等使用方式">
                <i class="fa fa-chevron-left"></i> cookie、localStorage和sessionStorage 三者之间的区别以及存储、获取、删除等使用方式
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/12/13/江湖再见/" rel="prev" title="江湖再见">
                江湖再见 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          
            <img class="site-author-image" itemprop="image"
              src="/uploads/hand1.jpg"
              alt="OBKoro1" />
          
            <p class="site-author-name" itemprop="name">OBKoro1</p>
            <p class="site-description motion-element" itemprop="description">种一棵树最好的时间是十年前,其次是现在</p>
        </div>

        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
            
              <a href="/archives">
            
                <span class="site-state-item-count">105</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">20</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">74</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/OBKoro1" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>GitHub</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://juejin.im/user/58714f0eb123db4a2eb95372" target="_blank" title="掘金">
                  
                    <i class="fa fa-fw fa-globe"></i>掘金</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://obkoro1.com/web_accumulate/" target="_blank" title="前端积累">
                  
                    <i class="fa fa-fw fa-globe"></i>前端积累</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://segmentfault.com/u/obkoro1/articles" target="_blank" title="segmentfault">
                  
                    <i class="fa fa-fw fa-globe"></i>segmentfault</a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#写在前面："><span class="nav-number">1.</span> <span class="nav-text">写在前面：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#引入步骤"><span class="nav-number">2.</span> <span class="nav-text">引入步骤</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装"><span class="nav-number">2.1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在src目录下创建文件夹vuex"><span class="nav-number">2.2.</span> <span class="nav-text">在src目录下创建文件夹vuex</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#vuex-index-js"><span class="nav-number">2.2.1.</span> <span class="nav-text">vuex/index.js</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vuex-modules-status-index-js"><span class="nav-number">2.3.</span> <span class="nav-text">vuex/modules/status/index.js</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vuex-modules-status-mutation-type-js"><span class="nav-number">2.4.</span> <span class="nav-text">vuex/modules/status/mutation_type.js</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vuex-modules-status-actions-js"><span class="nav-number">2.5.</span> <span class="nav-text">vuex/modules/status/actions.js</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vuex-modules-status-mutations-js"><span class="nav-number">2.6.</span> <span class="nav-text">vuex/modules/status/mutations.js</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#vuex文件夹，文件目录以及下载"><span class="nav-number">2.6.1.</span> <span class="nav-text">vuex文件夹，文件目录以及下载</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#引入main-js"><span class="nav-number">2.7.</span> <span class="nav-text">引入main.js</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#组件中如何使用。"><span class="nav-number">2.8.</span> <span class="nav-text">组件中如何使用。</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git地址"><span class="nav-number">3.</span> <span class="nav-text">git地址</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vuex简介"><span class="nav-number">4.</span> <span class="nav-text">vuex简介</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#vuex实现的作用：数据共享机制"><span class="nav-number">4.1.</span> <span class="nav-text">vuex实现的作用：数据共享机制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vuex的工作流程"><span class="nav-number">4.2.</span> <span class="nav-text">vuex的工作流程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#注意：这套模型是单向流动的"><span class="nav-number">4.3.</span> <span class="nav-text">注意：这套模型是单向流动的</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#后话"><span class="nav-number">5.</span> <span class="nav-text">后话</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2017 &mdash; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">OBKoro1</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">
      156.0k
    </span>
  
</div>




<div>
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span class="site-uv"><i class="fa fa-user"></i> 访问用户： <span class="busuanzi-value" id="busuanzi_value_site_uv">21621</span> 人 </span>
<span class="site-pv"><i class="fa fa-eye"></i> 访问次数： <span class="busuanzi-value" id="busuanzi_value_site_pv">68503</span> 次</span>

</div>



        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.2"></script>



  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>


  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  








  



  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("9ANFp60h6oVyxIPGlau3NOA4-gzGzoHsz", "tMdNuKMbFtN4QsekeNVu83TV");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

  

  

</body>
</html>

